package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

public class Update extends JFrame {

	private JPanel contentPane;
	private JTextField bname;
	private JTextField bpassword;
	private JTextField baddress;
	private JTextField bexplain;
	private JTextField bprice;
	private JTextField bdelvprive;
	private JTextField textField;
	private BusinessServiceImpl businessServiceImpl = new BusinessServiceImpl();
	private List<Business> list;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Update frame = new Update();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Update() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		bname = new JTextField();
		bname.setBounds(133, 36, 252, 21);
		contentPane.add(bname);
		bname.setColumns(10);
		
		bpassword = new JTextField();
		bpassword.setBounds(133, 67, 252, 21);
		contentPane.add(bpassword);
		bpassword.setColumns(10);
		
		baddress = new JTextField();
		baddress.setBounds(133, 98, 252, 21);
		contentPane.add(baddress);
		baddress.setColumns(10);
		
		bexplain = new JTextField();
		bexplain.setBounds(133, 129, 252, 21);
		contentPane.add(bexplain);
		bexplain.setColumns(10);
		
		bprice = new JTextField();
		bprice.setBounds(133, 163, 252, 21);
		contentPane.add(bprice);
		bprice.setColumns(10);
		
		bdelvprive = new JTextField();
		bdelvprive.setBounds(133, 194, 252, 21);
		contentPane.add(bdelvprive);
		bdelvprive.setColumns(10);
		
		JLabel name = new JLabel("\u5546\u5BB6\u59D3\u540D");
		name.setBounds(25, 39, 58, 15);
		contentPane.add(name);
		
		JLabel password = new JLabel("\u5BC6\u7801");
		password.setBounds(25, 70, 58, 15);
		contentPane.add(password);
		
		JLabel address = new JLabel("\u5546\u5BB6\u5730\u5740");
		address.setBounds(25, 101, 58, 15);
		contentPane.add(address);
		
		JLabel explain = new JLabel("\u5546\u5BB6\u4ECB\u7ECD");
		explain.setBounds(25, 132, 58, 15);
		contentPane.add(explain);
		
		JLabel price = new JLabel("\u8D77\u9001\u8D39");
		price.setBounds(25, 166, 58, 15);
		contentPane.add(price);
		
		JLabel delvprice = new JLabel("\u914D\u9001\u8D39");
		delvprice.setBounds(25, 197, 58, 15);
		contentPane.add(delvprice);
		
		JLabel lblNewLabel = new JLabel("\u8F93\u5165\u4FEE\u6539id");
		lblNewLabel.setBounds(133, 10, 103, 15);
		contentPane.add(lblNewLabel);
		
		JButton Commit = new JButton("\u786E\u5B9A");
		Commit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				businessServiceImpl = new BusinessServiceImpl();
				Business business = new Business();
				Integer id = Integer.valueOf(textField.getText());
				String name = bname.getText();
				String pass = bpassword.getText();
				String address = baddress.getText();
				System.out.println(address);
				String explain = bexplain.getText();
				Double starprice = Double.valueOf(bprice.getText());
				Double delv = Double.valueOf(bdelvprive.getText());
				business.setBusinessId(id);
				business.setBusinessName(name);
				business.setBusinessPassword(pass);
				business.setBusinessAddress(address);
				business.setBusinessExplain(explain);
				business.setStarPrice(starprice);
				business.setDeliveryPrice(delv);
				int i = businessServiceImpl.Update(business, id);
				if (i ==0){
					System.out.println("修改成功");
					MainWindow mainWindow = new MainWindow();
					BusinessTableModel businessTable = new BusinessTableModel();
					businessTable.setData(list);
					Update.this.dispose();
				}else {
					System.out.println("修改失败");
				}
			}
		});
		Commit.setBounds(77, 225, 97, 23);
		contentPane.add(Commit);
		
		JButton Clean = new JButton("\u6E05\u7A7A");
		Clean.setBounds(266, 225, 97, 23);
		contentPane.add(Clean);
		
		textField = new JTextField();
		textField.setBounds(209, 7, 40, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		this.setVisible(true);
	}

}
